package no.uis.archiva.consumer.liferay;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XmlHelper {
  
  private static final String DOCTYPE_SYSTEM = "http://www.liferay.com/dtd/liferay-plugin-repository_5_2_0.dtd";
  private static final String DOCTYPE_PUBLIC = "-//Liferay//DTD Plugin Repository 5.2.0//EN";
  
  public static Document createDocument(InputSource initialSource) throws ParserConfigurationException, SAXException, IOException { 
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    
    initialSource.setPublicId(DOCTYPE_PUBLIC);
    initialSource.setSystemId(DOCTYPE_SYSTEM);
    return docBuilder.parse(initialSource);
  }
  
  public static void writeNode(Node node, Writer writer) throws TransformerException {
    
    Source xmlSource = new DOMSource(node);
    xmlSource.setSystemId(DOCTYPE_SYSTEM);
    Result result = new StreamResult(writer);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, DOCTYPE_PUBLIC);
    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, DOCTYPE_SYSTEM);
    transformer.transform(xmlSource, result);
  }
  
  public static Element appendChildElement(Node parent, String tagName) {
    
    Document doc;
    if (parent instanceof Document) {
      doc = (Document)parent;
    } else {
      doc = parent.getOwnerDocument();
    }
    Element child = doc.createElement(tagName);
    parent.appendChild(child);

    return child;
  }
  
  public static Element appendTextElement(Node parent, String tagName, String data) {
    
    if (data == null || data.trim().isEmpty()) {
      return null;
    }
    
    Element elem = appendChildElement(parent, tagName);
    Text dataNode = elem.getOwnerDocument().createTextNode(data);
    elem.appendChild(dataNode);
    
    return elem;
  }
  
  public static Element appendListElement(Node parent, String tag, Collection<String> dataList) {
    if (dataList == null || dataList.isEmpty()) {
      return null;
    }
    Element listElem = appendChildElement(parent, tag+'s');
    for (String data : dataList) {
      appendTextElement(listElem, tag, data);
    }
    
    return listElem;
  }

  public static void addAttribute(Element node, String name, Object value) {
    if (value == null) {
      return;
    }
    String valString = String.valueOf(value);
    if (valString.trim().isEmpty()) {
      return;
    }
    node.setAttribute(name, valString);
  }

  
  private XmlHelper() {
  }
}
